package com.hq.utils;

import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.core5.http.ContentType;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.apache.hc.core5.http.io.entity.StringEntity;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.HashMap;
import java.util.Map;

@SpringBootTest
class HttpClientTest {
    @Autowired
    private HttpClient httpClient;
    private final  String url = "https://www.baidu.com";
    @Test
    void getString() {
        String s = httpClient.getString("https://www.ed9013.com/Enter/home.html");
        System.out.println(s);
    }
    @Test
    void getOrHead() {

        try {
            // 1、构建入参、添加 headers
            Map<String, String> headers = new HashMap<>();

            // 2、发起http请求
            CloseableHttpResponse httpResponse = httpClient.getOrHead(url, "GET", headers);

            // 3、返回结果，异常处理
            if (httpResponse.getCode() == 200) {
                String resStr = EntityUtils.toString(httpResponse.getEntity());
                System.out.println(resStr);
                System.out.println(httpResponse.getCode());
                System.out.println(httpResponse.getReasonPhrase());

            } else {


                System.out.println(httpResponse.getCode());
                System.out.println(httpResponse.getReasonPhrase());

            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }




    }

    @Test
    void post() {
        String url = "http://xxx.com.cn";
        try {
            // 1、构建入参、添加 headers
            StringEntity stringEntity = new StringEntity("JSONUtil.toJsonStr(req)", ContentType.APPLICATION_JSON);
            Map<String, String> headers = new HashMap<>();

            // 2、发起http请求
            CloseableHttpResponse httpResponse = httpClient.post(url, headers, stringEntity);

            // 3、返回结果，异常处理
            if (httpResponse.getCode() == 200) {
                String resStr = EntityUtils.toString(httpResponse.getEntity());
             //   log.info("request_success, response:{}, httpResponse_Code:{}, reasonPhrase:{}", resStr, httpResponse.getCode(), httpResponse.getReasonPhrase());
            } else {
                //log.error("request_fail, httpResponse_Code:{}, reasonPhrase:{}", httpResponse.getCode(), httpResponse.getReasonPhrase());
            }
        } catch (Exception e) {
            //log.error("request_udmp_fail, ex:{}", e);
        }

    }

    @Test
    void testPostFile(){
        /*
       MultipartFile multipartFile=new MultipartFile(){};
       String token;
       String key;


            String url = "http://xxx.com.cn";
            try {
                // 1、构建File参数
                File file = new File(multipartFile.getOriginalFilename());
                try (FileOutputStream fos = new FileOutputStream(file)) {
                    fos.write(multipartFile.getBytes());
                }
                MultipartEntityBuilder builder = MultipartEntityBuilder.create();
                builder.addBinaryBody("file", file, ContentType.MULTIPART_FORM_DATA, "ex.xlsx");

                // 2、构建其他参数
                builder.addTextBody("token", token, ContentType.TEXT_PLAIN);
                builder.addTextBody("key", key, ContentType.TEXT_PLAIN);
                HttpEntity multipartEntity = builder.build();

                // 3、需要加 header，在这里加
                Map<String, String> headers = new HashMap<>();

                // 4、发起http请求
                CloseableHttpResponse httpResponse = httpClient.post(url, headers, multipartEntity);

                // 5、返回结果，异常处理
                if (httpResponse.getCode() == 200) {
                    String resStr = EntityUtils.toString(httpResponse.getEntity());
                   // res = JSONUtil.toBean(resStr, UploadRes.class);
                  //  log.info("request_success, response:{}, httpResponse_Code:{}, reasonPhrase:{}", resStr, httpResponse.getCode(), httpResponse.getReasonPhrase());
                } else {
                  //  log.error("request_fail, httpResponse_Code:{}, reasonPhrase:{}", httpResponse.getCode(), httpResponse.getReasonPhrase());
                }
            } catch (Exception e) {
                //log.error("request_udmp_fail, ex:{}", e);
            }
            return ;
*/

    }



}